//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "AdaptedPathPayoff.h"
using namespace Cephei::QL::Experimental::Mcbasket;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Experimental/Mcbasket/PathPayoff.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::CAdaptedPathPayoff (boost::shared_ptr<QuantLib::AdaptedPathPayoff>& childNative, Object^ owner) : CPathPayoff(CAdaptedPathPayoff::typeid)
{
#ifdef HANDLE
	_phAdaptedPathPayoff = NULL;
#endif
	_ppAdaptedPathPayoff = &childNative;
    _ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (boost::dynamic_pointer_cast<QuantLib::PathPayoff> (*_ppAdaptedPathPayoff));
}
Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::CAdaptedPathPayoff (QuantLib::AdaptedPathPayoff& childNative, Object^ owner) : CPathPayoff(CAdaptedPathPayoff::typeid)
{
#ifdef HANDLE
	_phAdaptedPathPayoff = NULL;
#endif
	_ppAdaptedPathPayoff = new boost::shared_ptr<QuantLib::AdaptedPathPayoff> (&childNative);
    _ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (boost::dynamic_pointer_cast<QuantLib::PathPayoff> (*_ppAdaptedPathPayoff));
    _AdaptedPathPayoffOwner = owner;
    _PathPayoffOwner = owner;
}

Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::CAdaptedPathPayoff (CAdaptedPathPayoff^ copy) : CPathPayoff(CAdaptedPathPayoff::typeid)
{
#ifdef HANDLE
	_phAdaptedPathPayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppAdaptedPathPayoff = new boost::shared_ptr<QuantLib::AdaptedPathPayoff> (copy->GetShared());
        _ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (boost::dynamic_pointer_cast<QuantLib::PathPayoff> (*_ppAdaptedPathPayoff));
    }
}
Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::CAdaptedPathPayoff (System::Type^ t) : CPathPayoff(CAdaptedPathPayoff::typeid)
{
#ifdef HANDLE
	_phAdaptedPathPayoff = NULL;
#endif
	if (!t->IsSubclassOf(CAdaptedPathPayoff::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::CAdaptedPathPayoff (QuantLib::Handle<QuantLib::AdaptedPathPayoff>& childNative, Object^ owner)  : CPathPayoff(CAdaptedPathPayoff::typeid)
{
	_phAdaptedPathPayoff = &childNative;
	_ppAdaptedPathPayoff = &static_cast<boost::shared_ptr<QuantLib::AdaptedPathPayoff>>(childNative.currentLink());
    _ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (boost::dynamic_pointer_cast<QuantLib::PathPayoff> (*_ppAdaptedPathPayoff));
    _AdaptedPathPayoffOwner = owner;
}
Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::CAdaptedPathPayoff (QuantLib::Handle<QuantLib::AdaptedPathPayoff> childNative)  : CPathPayoff(CAdaptedPathPayoff::typeid)
{
	_phAdaptedPathPayoff = &childNative;
	_ppAdaptedPathPayoff = &static_cast<boost::shared_ptr<QuantLib::AdaptedPathPayoff>>(childNative.currentLink());
    _ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (boost::dynamic_pointer_cast<QuantLib::PathPayoff> (*_ppAdaptedPathPayoff));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::CAdaptedPathPayoff (QuantLib::AdaptedPathPayoff childNative)  : CPathPayoff(CAdaptedPathPayoff::typeid)
{
#ifdef HANDLE
	_phAdaptedPathPayoff = NULL;
#endif
	_ppAdaptedPathPayoff = new boost::shared_ptr<QuantLib::AdaptedPathPayoff> (new QuantLib::AdaptedPathPayoff (childNative));
    _ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (boost::dynamic_pointer_cast<QuantLib::PathPayoff> (*_ppAdaptedPathPayoff));
}
#endif

Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::~CAdaptedPathPayoff ()
{
    if (_ppAdaptedPathPayoff != NULL)
    {
	    delete _ppAdaptedPathPayoff;
        _ppAdaptedPathPayoff = NULL;
    }
}
Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::!CAdaptedPathPayoff ()
{
    if (_ppAdaptedPathPayoff != NULL)
    {
	    delete _ppAdaptedPathPayoff;
    }
}
QuantLib::AdaptedPathPayoff& Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::GetReference ()
{
    if (_ppAdaptedPathPayoff == NULL) throw gcnew NativeNullException ();
	return **_ppAdaptedPathPayoff;
}
boost::shared_ptr<QuantLib::AdaptedPathPayoff>& Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::GetShared ()
{
    if (_ppAdaptedPathPayoff == NULL) throw gcnew NativeNullException ();
	return *_ppAdaptedPathPayoff;
}
QuantLib::AdaptedPathPayoff* Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::GetPointer ()
{
    if (_ppAdaptedPathPayoff == NULL) throw gcnew NativeNullException ();
	return &**_ppAdaptedPathPayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::AdaptedPathPayoff>& Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::GetHandle ()
{
	if (_phAdaptedPathPayoff == NULL)
	{
		_phAdaptedPathPayoff = new Handle<QuantLib::AdaptedPathPayoff> (*_ppAdaptedPathPayoff);
	}
	return *_phAdaptedPathPayoff;
}
#endif
bool Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::HasNative () 
{
	return (_ppAdaptedPathPayoff != NULL);
}

Cephei::QL::Experimental::Mcbasket::IAdaptedPathPayoff^ Cephei::QL::Experimental::Mcbasket::CAdaptedPathPayoff::Value (Cephei::QL::Math::IMatrix^ path, Cephei::QL::Math::IArray^ payments, Cephei::QL::Math::IArray^ exercises, Cephei::IVector<Cephei::QL::Math::IArray^>^ states)
{
    CMatrix^ _Cpath;
    CArray^ _Cpayments;
    CArray^ _Cexercises;
    CoVector<Cephei::QL::Math::IArray^>^ _Cstates;
    try
    {
        _Cpath = safe_cast<CMatrix^> (path);
        _Cpath->Lock();
        QuantLib::Matrix& _path = static_cast<QuantLib::Matrix&> (_Cpath->GetReference ()); 
        _Cpayments = safe_cast<CArray^> (payments);
        _Cpayments->Lock();
        QuantLib::Array& _payments = static_cast<QuantLib::Array&> (_Cpayments->GetReference ()); 
        _Cexercises = safe_cast<CArray^> (exercises);
        _Cexercises->Lock();
        QuantLib::Array& _exercises = static_cast<QuantLib::Array&> (_Cexercises->GetReference ()); 
        _Cstates = safe_cast<CoVector<Cephei::QL::Math::IArray^>^> (states);
        _Cstates->Lock();
        INativeVector<Cephei::QL::Math::IArray^>^ _NCIstates = _Cstates->getFeature (NativeFeature::Value);
        CArrayVector^ _NCstates = safe_cast<CArrayVector^>(_NCIstates);
        std::vector<QuantLib::Array>& _states = static_cast<std::vector<QuantLib::Array>&> (_NCstates->GetReference ());
    	(*_ppAdaptedPathPayoff)->value ( _path,  _payments,  _exercises,  _states );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpath != nullptr) _Cpath->Unlock();
        if (_Cpayments != nullptr) _Cpayments->Unlock();
        if (_Cexercises != nullptr) _Cexercises->Unlock();
        if (_Cstates != nullptr) _Cstates->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

